﻿#region Copyright (C) Sector7 Software Studio. All rights reserved
/*******************************************************************************************
 * Creation
 *      Author:         Wolf Lai
 *      Date:           2013/6/6 星期四 22:51:30
 *      Version:        
 * Modification 
 *      Author: 
 *      Date: 
 *      Description: 
 *      Version:
 *******************************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Sector7.Medical.EntityType;
using Sector7.Framework.DB;
using Sector7.Medical.DAO.Utility;
using Sector7.Medical.Consts;

namespace Sector7.Medical.DAO.Standard
{
    /// <summary>
    /// 药品类型数据访问对象
    /// </summary>
    public class DrugTypeDAO : DAOBase<M7_DrugType>, IDrugTypeDAO
    {
        #region Public functions
        public override object Create(M7_DrugType drugType)
        {
            string sql = @"
                INSERT INTO M7_DrugType
                        (DrugTypeId
                        ,DrugTypeName
                        ,ParentId
                        ,Flags)
                    VALUES
                        (@DrugTypeId
                        ,@DrugTypeName
                        ,@ParentId
                        ,@Flags)";

            using (DBHelper helper = new DBHelper())
            {
                return helper.ExecuteNonQuery(sql,
                    DBUtil.CreateSqlParameter(drugType.DrugTypeId, "DrugTypeId"),
                    DBUtil.CreateSqlParameter(drugType.DrugTypeName, "DrugTypeName"),
                    DBUtil.CreateSqlParameter(drugType.ParentId, "ParentId"),
                    DBUtil.CreateSqlParameter(drugType.Flags, "Flags"));
            }
        }

        public List<M7_DrugType> ReadAllByFlags(int flags)
        {
            string sql = @"
                SELECT
                    parent.DrugTypeId pId,
                    parent.DrugTypeName pName,
                    parent.Flags pFlags,
                    child.DrugTypeId cId,
                    child.DrugTypeName cName,
                    child.Flags cFlags
                FROM M7_DrugType parent
                    LEFT JOIN M7_DrugType child ON child.ParentId = parent.DrugTypeId
                WHERE parent.Flags = @Flags";

            using (DBHelper helper = new DBHelper())
            {
                List<M7_DrugType> typeList = new List<M7_DrugType>();
                SqlDataReader reader = helper.ExecuteQuery(sql,
                    DBUtil.CreateSqlParameter(flags, "Flags"));
                
                M7_DrugType __parent = null;
                while (reader.Read())
                {
                    M7_DrugType parent = new M7_DrugType();
                    M7_DrugType child = new M7_DrugType();

                    object[] values = new object[reader.FieldCount];
                    reader.GetValues(values);
                    for (int i = 0; i < reader.FieldCount; ++i)
                    {
                        string propertyName = reader.GetName(i);
                        object propertyValue = values[i];

                        FillEntityUtil.ToPhysicalType(parent, propertyName, propertyValue);
                        FillEntityUtil.ToNatureType(child, propertyName, propertyValue);
                    }

                    if (__parent == null || __parent.DrugTypeId != parent.DrugTypeId)
                    {
                        __parent = parent;

                        typeList.Add(__parent);
                    }

                    if (child.DrugTypeId > 0)
                    {
                        if (__parent.Children == null)
                        {
                            __parent.Children = new List<M7_DrugType>();
                        }
                        __parent.Children.Add(child);
                    }
                }

                return typeList;
            }
        }
        #endregion

        #region Structure
        public DrugTypeDAO()
        {
            m_tableName = C_M7_TableName.DRUG_TYPE;
            m_primaryKeyName = C_ColumnName.DRUG_TYPE_ID;
        }
        #endregion
    }
}
